package com.athena.crm.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.athena.common.core.domain.entity.SysDept;
import com.athena.common.core.domain.model.LoginUser;
import com.athena.common.helper.LoginHelper;
import com.athena.crm.domain.bo.CrmClueTransferCustomerBo;
import com.athena.system.service.ISysDeptService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.athena.common.annotation.RepeatSubmit;
import com.athena.common.annotation.Log;
import com.athena.common.core.controller.BaseController;
import com.athena.common.core.domain.PageQuery;
import com.athena.common.core.domain.R;
import com.athena.common.core.validate.AddGroup;
import com.athena.common.core.validate.EditGroup;
import com.athena.common.core.validate.QueryGroup;
import com.athena.common.enums.BusinessType;
import com.athena.common.utils.poi.ExcelUtil;
import com.athena.crm.domain.vo.CrmCustomerVo;
import com.athena.crm.domain.bo.CrmCustomerBo;
import com.athena.crm.service.ICrmCustomerService;
import com.athena.common.core.page.TableDataInfo;

/**
 * CRM客户
 *
 * @author ruoyi
 * @date 2023-09-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/crm/customer")
public class CrmCustomerController extends BaseController {

    private final ICrmCustomerService iCrmCustomerService;

    private final ISysDeptService sysDeptService;

    /**
     * 查询CRM客户列表
     */
    @SaCheckPermission("crm:customer:list")
    @GetMapping("/list")
    public TableDataInfo<CrmCustomerVo> list(CrmCustomerBo bo, PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        return iCrmCustomerService.queryPageList(loginUser, bo, pageQuery);
    }

    /**
     * 导出CRM客户列表
     */
    @SaCheckPermission("crm:customer:export")
    @Log(title = "CRM客户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CrmCustomerBo bo, HttpServletResponse response) {
        List<CrmCustomerVo> list = iCrmCustomerService.queryList(bo);
        ExcelUtil.exportExcel(list, "CRM客户", CrmCustomerVo.class, response);
    }

    /**
     * 获取CRM客户详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("crm:customer:query")
    @GetMapping("/{id}")
    public R<CrmCustomerVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        CrmCustomerVo crmCustomerVo = iCrmCustomerService.queryById(id);
        if (crmCustomerVo != null) {
            SysDept sysDept = sysDeptService.selectDeptById(crmCustomerVo.getDeptId());
            crmCustomerVo.setDeptName(sysDept.getDeptName());
            if (crmCustomerVo.getFormerDeptId() != null) {
                SysDept sysDept1 = sysDeptService.selectDeptById(crmCustomerVo.getFormerDeptId());
                crmCustomerVo.setFormerDeptName(sysDept1.getDeptName());
            }
        }
        return R.ok(crmCustomerVo);
    }

    /**
     * 跟进条件获取CRM客户信息
     *
     * @param name 姓名
     * @param phone 电话
     * @return
     */
    @SaCheckPermission("crm:customer:query")
    @GetMapping("/{name}/{phone}")
    public R<CrmCustomerVo> getInfoByNameAndPhone(@NotNull(message = "姓名不能为空") @PathVariable String name,
                                                  @NotNull(message = "电话不能为空") @PathVariable String phone) {
        return R.ok(iCrmCustomerService.queryByNameAndPhone(name, phone));
    }

    /**
     * 新增CRM客户
     */
    @SaCheckPermission("crm:customer:add")
    @Log(title = "CRM客户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CrmCustomerBo bo) {
        return toAjax(iCrmCustomerService.insertByBo(bo));
    }

    /**
     * 修改CRM客户
     */
    @SaCheckPermission("crm:customer:edit")
    @Log(title = "CRM客户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CrmCustomerBo bo) {
        return toAjax(iCrmCustomerService.updateByBo(bo));
    }

    /**
     * 删除CRM客户
     *
     * @param ids 主键串
     */
    @SaCheckPermission("crm:customer:remove")
    @Log(title = "CRM客户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iCrmCustomerService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
